package streamforce.crypto.cloud.subscriber;

import streamforce.crypto.KPAbe;
import streamforce.crypto.cloud.EPCloud;
import streamforce.crypto.event.Ciphertext;
import streamforce.crypto.event.StockEvent;
import streamforce.crypto.event.StockEventJoin;
import streamforce.crypto.policies.Policy;

/**
 * First step in the Join process, i.e. listening to
 * 	select * from StockEvent(stockId=0/1)
 * and then transform the deterministic encryption of the price field. 
 * Finally, we forward it to another stream of StockEventJoin
 */
public class CloudJoinVolumeStage1 extends Policy{
	
	int policyId; 
	
	public CloudJoinVolumeStage1(int pi, KPAbe ch){
		super(ch); 
		this.policyId = pi; 
	}
	
	public  void update(StockEvent se){
		byte[] ciphertext = se.getVolume().get("volume_det").getCipher();
		byte[] trans = this.cryptoHandler.transform_join(se.getStockId(), this.policyId, ciphertext);
		se.getVolume().put("volume_det", new Ciphertext(trans));
		
		StockEventJoin sej = new StockEventJoin(se.getTimestamp(),se.getRealTime(),
				se.getStockId(), se.getPrice(), se.getVolume());
		EPCloud.getEPServiceProvider().getEPRuntime().sendEvent(sej);
	}		
}
